import axios from 'axios';

const prefix = process.env.NODE_ENV === 'development' ? '/api' : '';
const course_list = prefix + '/knowledge/course/list';
const course_my_list = prefix + '/knowledge/course/my/list';
const create_course = prefix + '/knowledge/course/save';
const course_detail = prefix + '/knowledge/course/detail/';

export default {
  fetchCourseList: function (parameters, callback, errorHandler) {
    axios.get(course_list, {
      params: {
        categoryId: parameters[0],
        orderBy: parameters[1],
        pageNum: parameters[2],
        pageSize: parameters[3]
      }
    }).then((resp)=> {
      callback(resp.data.coursePageDTO)
    }).catch((resp)=> {
      errorHandler(resp)
    });
  },

  fetchMyCourseList: function (callback, errorHandler) {
    axios.get(course_my_list)
      .then((resp)=> {
        callback(resp.data.courseDTOList);
      }).catch((resp)=> {
        errorHandler(resp);
      });
  },

  createCourse: function (parameters, callback, errorHandler) {
    axios.post(create_course, {
      categoryId: parameters[0],
      topic: parameters[1],
      profile: parameters[2],
      poster: parameters[3]
    }).then((resp)=> {
      callback(resp.data);
    }).catch((resp)=> {
      errorHandler(resp);
    });
  },

  fetchCourseDetail: function (parameter, callback, errorHandler) {
    axios.get(course_detail + parameter).then((resp)=> {
      callback(resp.data.courseDTO);
    }).catch((resp)=> {
      errorHandler(resp);
    });
  },

  view_course: prefix + '/knowledge/course/view'

}
